package com.ruoyi.mall.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.domain.MallCustomer;
import com.ruoyi.mall.service.IMallCustomerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户管理Controller
 * 
 * @author banana_slices
 * @date 2024-12-01
 */
@Api(tags = "客户接口")
@RestController
@RequestMapping("/mall/customer")
public class MallCustomerController extends BaseController
{
    @Autowired
    private IMallCustomerService mallCustomerService;

    /**
     * 查询客户管理列表
     */
    @ApiOperation(value = "查询")
    @PreAuthorize("@ss.hasPermi('mall:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallCustomer mallCustomer)
    {
        startPage();

        if (getUserId() != 1)
        {
            //若userId不为1则添加查询限制
            mallCustomer.setCreateBy(getUsername());
        }

        List<MallCustomer> list = mallCustomerService.selectMallCustomerList(mallCustomer);

        return getDataTable(list);
    }

    /**
     * 导出客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('mall:customer:export')")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallCustomer mallCustomer)
    {
        List<MallCustomer> list = mallCustomerService.selectMallCustomerList(mallCustomer);
        ExcelUtil<MallCustomer> util = new ExcelUtil<MallCustomer>(MallCustomer.class);
        util.exportExcel(response, list, "客户管理数据");
    }

    /**
     * 获取客户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:customer:query')")
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") Long customerId)
    {
        return success(mallCustomerService.selectMallCustomerByCustomerId(customerId));
    }

    /**
     * 新增客户管理
     */
    @PreAuthorize("@ss.hasPermi('mall:customer:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallCustomer mallCustomer)
    {
        mallCustomer.setCreateBy(getUsername());
        return toAjax(mallCustomerService.insertMallCustomer(mallCustomer));
    }

    /**
     * 修改客户管理
     */
    @PreAuthorize("@ss.hasPermi('mall:customer:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallCustomer mallCustomer)
    {
        return toAjax(mallCustomerService.updateMallCustomer(mallCustomer));
    }

    /**
     * 删除客户管理
     */
    @PreAuthorize("@ss.hasPermi('mall:customer:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customerIds}")
    public AjaxResult remove(@PathVariable Long[] customerIds)
    {
        return toAjax(mallCustomerService.deleteMallCustomerByCustomerIds(customerIds));
    }
}
